from django.shortcuts import render

# Create your views here.
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView

from goods.models import SKU
from goods.serializers import SKUIndexSerializer
from users.serializerss import SKUSerializer

#　获取三级分类下的商品数据
# GET /categories/(?P<category_id>\d+)/skus/
class SKUListView(ListAPIView):
    serializer_class =  SKUSerializer
    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id)
    # 排序
    filter_backends = [OrderingFilter]
    # 制定排序字段
    ordering_fields = ['price','create_time','sales']


#　GET /skus/search/?text<搜索关键字>
class SKUSearchViewSet(HaystackViewSet):
    # 指定索引类对应模型类
    index_models = [SKU]
    # 指定搜索结果序列化时所使用的序列化器类
    # 搜索结果重每个对象都包含两个属性：
    # text : 索引字段的内容
    # object: 从数据库中搜索模型对象（此处是SKU模型对象）
    serializer_class = SKUIndexSerializer